﻿using DetectScars.model;
using ScottPlot;
using ScottPlot.MultiplotLayouts;
using ScottPlot.PlotStyles;
using ScottPlot.Plottables;
using ScottPlot.TickGenerators.TimeUnits;
using ScottPlot.WinForms;
using System;
using System.Runtime.Intrinsics.Arm;
using System.Windows.Forms;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static System.Windows.Forms.AxHost;

namespace DetectScars.common
{
    class SocttPlotUtils
    {
        static FormsPlot? formsPlot_;
        static FormsPlot? formsPlot3_;
        static FormsPlot? formsPlot5_;
        static FormsPlot? formsPlot2_;
        static FormsPlot? formsPlot4_;
        static int count = 0;
        internal static bool drag_flag = false;
        static bool image_enable = true;
        //第一个闸门的信号值
        public static int dac_gate_xh = 0;

        //绘制dac曲线开关
        internal static void SetDacImage(bool enable, double y)
        {
            if (formsPlot_ == null)
            {
                return;
            }
            formsPlot_.Plot.Clear<HorizontalLine>();
            if (enable)
            {
                var axLine2 = formsPlot_.Plot.Add.HorizontalLine(y);
                axLine2.Text = "DAC";
            }
        }
        //图像增强
        internal static void SetImage(bool enable)
        {
            image_enable = enable;
        }
        //设置闸门信号
        internal static void SetDacGateXh(int xh)
        {
            dac_gate_xh = xh;
        }
        //初始化
        public static void Init(FormsPlot formsPlot, FormsPlot formsPlot3, FormsPlot formsPlot5, FormsPlot formsPlot2)
        {
            formsPlot_ = formsPlot;
            formsPlot3_ = formsPlot3;
            formsPlot5_ = formsPlot5;
            formsPlot2_ = formsPlot2;
            //formsPlot4_ = formsPlot4;
        }

        static int BAindex = 0;
        static double CindexV = 0;
        static double CindexH = 0;
        static double PWII_index = 0;
        internal static void SetBAindex(double pIndex)
        {
            PWII_index = pIndex;
            BAindex = (int)pIndex;

        }
        internal static void SetBAindexV(double pIndex)
        {
            CindexV = pIndex;
            int y = (int)CindexV;
            int x = 160 - (int)CindexH;
            DataAandC data = new DataAandC();
            if (x < 0|| x>= AcqData.dataCA1.GetLength(0))
            {
                return;
            }
            if (y<0||y >= AcqData.dataCA1.GetLength(1))
            {
                return;
            }
            data.U10_DataA = AcqData.dataCA1[x, y];
            DisplayData(data);
        }
        internal static void SetBAindexH(double pIndex)
        {
            CindexH = pIndex;
            int y = (int)CindexV;
            int x = 160 - (int)CindexH;
            DataAandC data = new DataAandC();
            if (x < 0 || x >= AcqData.dataCA1.GetLength(0))
            {
                return;
            }
            if (y < 0 || y >= AcqData.dataCA1.GetLength(1))
            {
                return;
            }
            data.U10_DataA = AcqData.dataCA1[x, y];
            DisplayData(data);

        }
        //刷新闸门
        internal static void RefreshGates(double st,double et,double h,ScottPlot.Color color,double sd)
        {
            if (formsPlot_ == null)
            {
                return;
            }
            for (int i = 0; i < formsPlot_.Plot.PlottableList.Count; i++)
            {
                if (formsPlot_.Plot.PlottableList[i] is not LinePlot)
                {
                    continue;
                }
                LinePlot linePlot_ = (LinePlot)formsPlot_.Plot.PlottableList[i];
                if (linePlot_.Color == color)
                {
                    formsPlot_.Plot.PlottableList.Remove(linePlot_);
                    break;
                }
            }
            LinePlot line = formsPlot_.Plot.Add.Line(new Coordinates(st, h), new Coordinates(et, h));
            line.Color = color;
            formsPlot_.Refresh();
        }
        internal static void RefreshGates2(DataAandC gates, double st, double val)
        {
            if (formsPlot_ == null || gates ==null)
            {
                return;
            }
            if (drag_flag)
            {
                return;
            }
            double h1 = 0;
            double h2 = 0;
            double h3 = 0;
            for (int i = 0; i < formsPlot_.Plot.PlottableList.Count; i++)
            {
                if (formsPlot_.Plot.PlottableList[i] is not LinePlot)
                {
                    continue;
                }
                LinePlot linePlot = (LinePlot)formsPlot_.Plot.PlottableList[i];
                double h = linePlot.Start.Y;
                if (linePlot.Color == Colors.C0)
                {
                    h1 = h;
                }else if (linePlot.Color == Colors.C1)
                {
                    h2 = h;
                }
                else if (linePlot.Color == Colors.C2)
                {
                    h3 = h;
                }
            }
            
            formsPlot_.Invoke(new Action(() =>
            {
                formsPlot_.Plot.Clear<LinePlot>();
                LinePlot line1 = formsPlot_.Plot.Add.Line(new Coordinates(st + gates.U1034_CH_DR0_VIEW_FIRST * val / 511, h1), new Coordinates(st + gates.U1036_CH_DR0_VIEW_LAST * val / 511, h1));
                line1.Color = Colors.C0;
                LinePlot line2 = formsPlot_.Plot.Add.Line(new Coordinates(st + gates.U1048_CH_DR1_VIEW_FIRST * val / 511, h2), new Coordinates(st + gates.U1050_CH_DR1_VIEW_LAST * val / 511, h2));
                line2.Color = Colors.C1;
                LinePlot line3 = formsPlot_.Plot.Add.Line(new Coordinates(st + gates.U1062_CH_DR2_VIEW_FIRST * val / 511, h3), new Coordinates(st + gates.U1064_CH_DR2_VIEW_LAST * val / 511, h3));
                line3.Color = Colors.C2;
                formsPlot_.Refresh();
            }));
        }
        //pautA扫
        internal static void DisplayData(DataAandC dataA)
        {
            try
            {
                //解析数据
                if (dataA==null || dataA.U10_DataA == null)
                {
                    return;
                }
                int index = 0;
                

                if (formsPlot_ == null)
                {
                    return;
                }
                
                //创建一个推托事件让formsPlot_更新数据
                formsPlot_.Invoke(new Action(() =>
                {
                    formsPlot_.Plot.Clear<Signal>();
                    double cy = (formsPlot_.Plot.Axes.Bottom.Max - formsPlot_.Plot.Axes.Bottom.Min) / 512;
                    Signal singal = formsPlot_.Plot.Add.Signal(dataA.U10_DataA, cy);
                    singal.Data.XOffset = formsPlot_.Plot.Axes.Bottom.Min;
                    singal.Color = Colors.C0;
                    formsPlot_.Refresh();
                }));
            }
            catch (Exception e)
            {
                LogUtils.WriteLog(e.Message);
                MessageBox.Show(e.Message);
            }
        }
        static bool flag_drow=true;
        static bool flag_drow_c = true;
        static bool flag_drow_d = true;
        public static double[,] dataC1;
        private static Heatmap heatmap_c1;
        //pautC扫
        internal static void DisplayDataC1(double[,] dataY, double height,float weight,double left_max)
        {
            try
            {
                //解析数据
                if (dataY == null)
                {
                    return;
                }

                if (formsPlot3_ == null)
                {
                    return;
                }
                //创建一个推托事件让formsPlot_更新数据
                if (dataC1 == null)
                {
                    dataC1 = dataY;
                    formsPlot3_.Plot.Clear<Heatmap>();
                    heatmap_c1 = formsPlot3_.Plot.Add.Heatmap(dataC1);
                    heatmap_c1.Position = new(0.0, weight, 0, height);
                    heatmap_c1.Colormap = new ScottPlot.Colormaps.Jet();
                    heatmap_c1.ManualRange = new(0, left_max);
                    formsPlot3_.Plot.MoveToBottom(heatmap_c1);
                    if (flag_drow_c)
                    {
                        flag_drow_c = false;
                        formsPlot3_.Plot.Add.ColorBar(heatmap_c1);
                    }
                }
                else
                {
                    dataC1 = dataY;
                    heatmap_c1.Update();
                    
                }
                formsPlot3_.Invoke(new Action(() =>
                {
                    formsPlot3_.Refresh();
                }));
                
            }
            catch (Exception e)
            {
                LogUtils.WriteLog(e.Message);
                MessageBox.Show(e.Message);
            }
        }

        //pautC扫
        public static double[,] dataC2;
        private static Heatmap heatmap_c2;
        internal static void DisplayDataC2(double[,] dataY, double height, float weight, double left_max)
        {
            try
            {
                //解析数据
                if (dataY == null)
                {
                    return;
                }

                if (formsPlot2_ == null)
                {
                    return;
                }
                //Random random = new Random();
                //for (int i = 0; i < 10; i++)
                //{
                //    for (int j = 0; j < 6965; j++)
                //        dataY[i, j] = random.Next(512);
                //}
                //创建一个推托事件让formsPlot_更新数据
                if (dataC2 == null)
                {
                    dataC2 = dataY;
                    formsPlot2_.Plot.Clear<Heatmap>();
                    heatmap_c2 = formsPlot2_.Plot.Add.Heatmap(dataC2);
                    heatmap_c2.Position = new(0.0, weight, 0, height);
                    heatmap_c2.Colormap = new ScottPlot.Colormaps.Jet();
                    heatmap_c2.ManualRange = new(0, 512);
                    formsPlot2_.Plot.MoveToBottom(heatmap_c2);
                    if (flag_drow_c)
                    {
                        flag_drow_c = false;
                        formsPlot2_.Plot.Add.ColorBar(heatmap_c2);
                    }

                }
                else
                {
                    dataC2 = dataY;
                    heatmap_c2.Update();
                }
                formsPlot2_.Invoke(new Action(() =>
                {
                    
                    formsPlot2_.Refresh();
                }));
                
            }
            catch (Exception e)
            {
                LogUtils.WriteLog(e.Message);
                MessageBox.Show(e.Message);
            }
        }
        //pautC扫A扫
        internal static void DisplayDataCA(double left_max, float Z_PRE_MM)
        {
            try
            {
                if (formsPlot3_ == null || formsPlot_ == null)
                {
                    return;
                }
                //绘制A扫
                int index_x = (int)((formsPlot3_.Plot.Axes.Left.Max - 50 - CindexH) * 5 / AcquisitionData.encoder_z);//DATA_A的横坐标
                int index_x2 = (int)((formsPlot3_.Plot.Axes.Left.Max - 50 - CindexH - index_x * AcquisitionData.encoder_z / 5) / Z_PRE_MM);//
                if (index_x < 0 || index_x >= AcquisitionData.M)
                {
                    return;
                }
                if (index_x2 < 0 || index_x2 >= AcquisitionData.ROW)
                {
                    return;
                }
                int index_y = (int)(CindexV / AcquisitionData.J);
                if (index_y < 0 || index_y >= AcquisitionData.N)
                {
                    return;
                }
                int[] flatData = AcquisitionData.DATA_A[index_x, index_y];
                if (flatData == null)
                {
                    return;
                }
                double[] result = new double[AcquisitionData.COL];
                for (int j = 0; j < AcquisitionData.COL; j++)
                {
                    result[j] = flatData[index_x2 * AcquisitionData.COL + j] * 100 / left_max;
                }
                if (formsPlot5_ == null)
                {
                    return;
                }
                formsPlot5_.Invoke(new Action(() =>
                {
                    formsPlot5_.Plot.Clear<Signal>();
                    //采样率
                    double cy = double.Parse(formsPlot_.Tag.ToString()) / AcquisitionData.COL;
                    Signal singal = formsPlot5_.Plot.Add.Signal(result, cy);
                    singal.Data.XOffset = formsPlot5_.Plot.Axes.Bottom.Min;
                    formsPlot5_.Refresh();
                }));
            }
            catch (Exception e)
            {
                LogUtils.WriteLog(e.Message);
                MessageBox.Show(e.Message);
            }
        }

        //tcg线
        internal static void SetTCG(bool enable, double base_zy,double cur_zy)
        {
            if (formsPlot_ == null)
            {
                return;
            }
            double y = 80 * Math.Pow(10, (cur_zy - base_zy) / 20);
            formsPlot_.Plot.Clear<HorizontalLine>();
            if (enable)
            {
                var axLine2 = formsPlot_.Plot.Add.HorizontalLine(y);
                axLine2.Text = "TCG";
                
            }
            formsPlot_.Refresh();
        }
        //dac线
        internal static void SetDAC(bool enable, double[] x, float[] y)
        {
            if (formsPlot_ == null||x==null)
            {
                return;
            }
            formsPlot_.Plot.Clear<Scatter>();
            if (enable)
            {
                Scatter scatter = formsPlot_.Plot.Add.ScatterLine(x, y);
                //Scatter scatter = formsPlot_.Plot.Add.ScatterLine(xs, ys);
                scatter.MarkerShape = MarkerShape.FilledSquare;
                //scatter.LegendText = "DAC";
                
            }
            formsPlot_.Refresh();
        }

    }
}
